﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DecisionTree
{
    class PyramidCreater
    {
        public static byte[,] Convert2D(byte[] image)
        {
            byte[,] rVal = new byte[(int)Math.Sqrt(image.Length), (int)Math.Sqrt(image.Length)];

            int lastIndex = 0;
            for (int i = 0; i < rVal.GetLength(0); i++)
            {
                for (int j = 0; j < rVal.GetLength(1); j++)
                {
                    rVal[i, j] = image[lastIndex];
                    lastIndex++;
                }
            }

            return rVal;
        }

        static byte[,] Make32x32(byte[] array)
        {
            byte[,] image = Convert2D(array);

            byte[,] rVal = new byte[32, 32];

            for (int i = 0; i < rVal.GetLength(0); i++)
                for (int j = 0; j < rVal.GetLength(1); j++)
                    if (!(i < 2 || j < 2 || i > 29 || j > 29))
                        rVal[i, j] = image[i - 2, j - 2];

            return rVal;
        }

        static byte[,] DownSample(byte[,] image, int numDownSample)
        {
            int ratio = (int)Math.Pow(2, numDownSample);
            byte[,] rVal = new byte[image.GetLength(0) / ratio, image.GetLength(1) / ratio];

            for (int i = 0; i < rVal.GetLength(0); i++)
            {
                for (int j = 0; j < rVal.GetLength(1); j++)
                {
                    double sum = 0;

                    for (int k = i * ratio; k < i * ratio + ratio; k++)
                    {
                        for (int l = j * ratio; l < j * ratio + ratio; l++)
                        {
                            sum += image[k, l];
                        }
                    }

                    rVal[i, j] = Convert.ToByte(sum / Math.Pow(ratio, 2));
                }
            }

            return rVal;
        }

        static void WriteImage(byte[,] image, string path)
        {
            Image img = new Image(image.GetLength(1), image.GetLength(1));

            for (int i = 0; i < img.Width; i++)
            {
                for (int j = 0; j < img.Height; j++)
                {
                    img.SetPixel(i, j, image[j, i]);
                }
            }

            img.WriteImage(path);
        }

        public static byte[] Convert1D(byte[,] matrix)
        {
            byte[] rVal = new byte[matrix.GetLength(0) * matrix.GetLength(1)];

            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    rVal[i * matrix.GetLength(1) + j] = matrix[i, j];
                }
            }

            return rVal;
        }

        public static void Dowork()
        {
            byte[][] inputs = FileOperations.ReadImages("C:\\testInputs.data");

            byte[][] rVal = new byte[inputs.Length][];

            for (int i = 0; i < inputs.Length; i++)
            {
                byte[,] filled = Make32x32(inputs[i]);

                //byte[] a4x4 = Convert1D(DownSample(filled, 3));
                byte[] a8x8 = Convert1D(DownSample(filled, 2));
                byte[] a16x16 = Convert1D(DownSample(filled, 1));
                byte[] a32x32 = Convert1D(filled);

                byte[] concated = new byte[a8x8.Length + a16x16.Length + a32x32.Length];
                a8x8.CopyTo(concated, 0);
                a16x16.CopyTo(concated, a8x8.Length);
                a32x32.CopyTo(concated, a8x8.Length + a16x16.Length);

                rVal[i] = concated;
            }

            DBWriter.WriteDB(rVal, "C:\\testPyramid.data");
        }
    }
}
